﻿namespace IosDecode
{
    /// <summary>
    ///     加密解密类
    /// </summary>
    internal class EncodeAndDecodeClass
    {
        private static readonly byte[] CharMaps =
        {
                  0x61, 0x67, 0x62, 0x78, 0x63, 0x77, 0x64, 0x76, 0x65,
                  0x66, 0x66, 0x75, 0x67, 0x74, 0x68, 0x7A, 0x69, 0x73,
                  0x6A, 0x68, 0x6B, 0x65, 0x6C, 0x69, 0x6D, 0x79, 0x6E,
                  0x72, 0x6F, 0x64, 0x70, 0x63, 0x71, 0x71, 0x72, 0x62,
                  0x73, 0x6A, 0x74, 0x70, 0x75, 0x6B, 0x76, 0x61, 0x77,
                  0x6C, 0x78, 0x6D, 0x79, 0x6F, 0x7A, 0x6E, 0x41, 0x4F,
                  0x42, 0x4D, 0x43, 0x45, 0x44, 0x4E, 0x45, 0x50, 0x46,
                  0x44, 0x47, 0x51, 0x48, 0x4B, 0x49, 0x43, 0x4A, 0x4C,
                  0x4B, 0x53, 0x4C, 0x55, 0x4D, 0x4A, 0x4E, 0x42, 0x4F,
                  0x52, 0x50, 0x54, 0x51, 0x41, 0x52, 0x49, 0x53, 0x47,
                  0x54, 0x46, 0x55, 0x5A, 0x56, 0x58, 0x57, 0x57, 0x58,
                  0x56, 0x59, 0x48, 0x5A, 0x59, 0x30, 0x33, 0x31, 0x39,
                  0x32, 0x35, 0x33, 0x32, 0x34, 0x34, 0x35, 0x30, 0x36,
                  0x37, 0x37, 0x31, 0x38, 0x36, 0x39, 0x38, 0x7B, 0x28,
                  0x7D, 0x29, 0x28, 0x3A, 0x29, 0x7B, 0x2E, 0x7D, 0x3A,
                  0x20, 0x20, 0x5F, 0x5F, 0x2C, 0x2C, 0xA, 0xA, 0x2E
        };

        private static bool _charMapInited;
        private static readonly byte[] EncodedToPlain = new byte[256];
        private static readonly byte[] PlainToEncoded = new byte[256];

        private static byte[] charMapList = new byte[256];

        private static void BuildEncryptMap()
        {
            var v1 = 0;
            if (!_charMapInited)
            {
                do
                {
                    EncodedToPlain[CharMaps[v1 + 1]] = CharMaps[v1];
                    PlainToEncoded[CharMaps[v1]] = CharMaps[v1 + 1];
                    v1 += 2;
                } while (v1 < 0x90);
                _charMapInited = true;
            }
        }

        private static char ToDecode(char v)
        {
            BuildEncryptMap();
            if (v >= 256) return v;
            if (EncodedToPlain[v] == 0) return v;
            return (char)EncodedToPlain[v];
        }


        private static char ToEncoded(char v)
        {
            BuildEncryptMap();
            if (v >= 256) return v;
            if (PlainToEncoded[v] == 0) return v;
            return (char)PlainToEncoded[v];
        }

        /// <summary>
        ///     解密方法
        /// </summary>
        public static string ToDecode(string v)
        {
            var r = new char[v.Length];
            for (var i = 0; i < v.Length; ++i)
                r[i] = ToDecode(v[i]);
            return new string(r);
        }

        /// <summary>
        ///     加密方法
        /// </summary>
        public static string ToEncoded(string v)
        {
            var r = new char[v.Length];
            for (var i = 0; i < v.Length; ++i)
                r[i] = ToEncoded(v[i]);
            return new string(r);
        }
    }
}